<template>
  <div class="app-container">
    <div class="app-table">
      <div class="addButton" style="display: none;"><el-button type="primary"></el-button></div>
      <!-- <el-table class="my-custom-table" v-loading="listLoading" :data="tableData" element-loading-text="Loading" he
        border fit highlight-current-row :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"> -->
      <el-table v-loading="listLoading" element-loading-text="Loading" class="my-custom-table" :data="tableData"
        style="width: 100%;margin-bottom: 20px;" row-key="id" border default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column label="序号" sortable width="180" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="菜单名称" sortable width="180" align="center">
        </el-table-column>
        <el-table-column prop="code" label="路由" align="center">
        </el-table-column>
        <el-table-column prop="component" label="组件" align="center">
        </el-table-column>
        <el-table-column label="按钮" width="80" align="center">
          <template slot-scope="scope">
            <el-tag> {{ scope.row.isButton ? "是" : "否" }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="隐藏菜单" width="110" align="center">
          <template slot-scope="scope">
            <el-tag> {{ scope.row.isHide ? "是" : "否" }}</el-tag>
          </template>

        </el-table-column>
        <el-table-column prop="orderSort" label="排序" align="center">
        </el-table-column>
        <el-table-column prop="description" label="菜单描述" align="center">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button @click="openAddDialog(scope.row.id)" type="text" size="small">
              新增
            </el-button>
            <el-button @click="openAddUpdDialog(scope.row)" type="text" size="small">
              编辑
            </el-button>
            <el-button @click="openDelDialog(scope.row.id)" type="text" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="title" :visible.sync="addOrUpdateDialog" center v-if="addOrUpdateDialog">
      <el-form :model="menuObject" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="编号" v-show="false">
          <el-input v-model="menuObject.id"></el-input>
        </el-form-item>
        <el-form-item label="是否按钮" prop="isButton">
          <template>
            <el-radio-group v-model="menuObject.isButton">
              <el-radio :label=false>菜单</el-radio>
              <el-radio :label=true>按钮</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item label="按钮名称" prop="name" v-show="menuObject.isButton">
          <el-input v-model="menuObject.name"></el-input>
        </el-form-item>
        <el-form-item label="接口地址" prop="moduleId" v-show="menuObject.isButton">
          <el-input v-model="menuObject.moduleId"></el-input>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name" v-show="!menuObject.isButton">
          <el-input v-model="menuObject.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单路由" prop="code" v-show="!menuObject.isButton">
          <el-input v-model="menuObject.code"></el-input>
        </el-form-item>
        <el-form-item label="菜单组件" prop="component" v-show="!menuObject.isButton">
          <el-input v-model="menuObject.component"></el-input>
        </el-form-item>

        <el-form-item label="菜单排序" prop="orderSort" v-show="!menuObject.isButton">
          <el-input v-model="menuObject.orderSort"></el-input>
        </el-form-item>
        <el-form-item label="菜单描述" prop="description" v-show="!menuObject.isButton">
          <el-input v-model="menuObject.description"></el-input>
        </el-form-item>
        <el-form-item label="隐藏菜单" prop="isHide" v-show="!menuObject.isButton">
          <el-switch v-model="menuObject.isHide"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addOrUpdateDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitMenuInfo('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { queryMenus, createPermissions, updatePermissions,deletePermissions } from '@/api/menu'
import { Message } from 'element-ui'
export default {
  name: 'Dashboard',
  components: {},
  data() {
    return {
      dialogVisible: false,
      deleteId: null,
      tableData: [],
      listLoading: false,
      parentId: null,
      menuObject: {
        isButton: false,
        orderSort: 0,
        parentId: null
      },
      addOrUpdateDialog: false,
      title: "",
      menuRules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '请输入菜单路由', trigger: 'change' }
        ],
        component: [
          { required: true, message: '请输入菜单组件', trigger: 'change' }
        ],
        orderSort: [
          { required: true, message: '请输入菜单排序', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入菜单描述', trigger: 'change' }
        ]
      },
      buttonRules: {
        name: [
          { required: true, message: '请输入按钮名称', trigger: 'blur' },
        ],
        moduleId: [
          { required: true, message: '请输入接口地址', trigger: 'change' }
        ]
      },
      rules: {}
    }
  },
  watch: {
    'menuObject.isButton': {
      handler(newVal, oldVal) {
        if (newVal) {
          this.rules = this.buttonRules
        } else {
          this.rules = this.menuRules
        }
      }
    },
  },
  created() {
    this.getList()
    this.rules = this.menuRules
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { data } = await queryMenus(this.parentId)
      this.tableData = data
      this.listLoading = false
      console.log(11111111111, this.tableData)
    }, openAddDialog(parentId) {
      this.menuObject = {
        isButton: false,
        orderSort: 0,
        parentId: parentId,
        id: null
      }
      this.title = "新增菜单信息"
      this.addOrUpdateDialog = true
    }
    , openAddUpdDialog(row) {
      if (row) {
        this.title = "修改菜单信息"
        //深拷贝一下
        this.menuObject = JSON.parse(JSON.stringify(row))
      } else {
        this.menuObject = {
        }
        this.title = "新增菜单信息"
      }
      this.addOrUpdateDialog = true
    },
    async submitMenuInfo(formName) {
      this.$refs[formName].validate(async (valid) => {
        console.log(33333333, this.$refs[formName])
        if (valid) {
          if (this.menuObject.isButton) {
            this.menuObject = {
              isButton: this.menuObject.isButton,
              orderSort: this.menuObject.orderSort,
              parentId: this.menuObject.parentId,
              id: this.menuObject.id,
              name: this.menuObject.name,
              moduleId: this.menuObject.moduleId
            }
          }
          if (this.menuObject.id) {
            await updatePermissions(this.menuObject).then(res => {
              this.listLoading = false
              this.addOrUpdateDialog = !res;
              if (res) {
                Message({
                  message: "修改成功",
                  type: 'info',
                  duration: 5 * 1000
                })
                this.getList();
              }
            }).catch(err => {
              this.listLoading = false
              this.addOrUpdateDialog = false;
            })
          } else {
            await createPermissions(this.menuObject).then(res => {
              this.listLoading = false
              this.addOrUpdateDialog = !res;
              if (res) {
                Message({
                  message: "新增成功",
                  type: 'success',
                  duration: 5 * 1000
                })
                this.getList();
              }
            }).catch(err => {
              this.listLoading = false
              this.addOrUpdateDialog = false;
            })
          }
        }
      });
    },
    async deleteMenu() {
      this.listLoading = true
      await deletePermissions({ Id: this.deleteId }).then(res => {
        this.listLoading = false
        this.dialogVisible = !res;
        if (res) {
          Message({
            message: "删除成功",
            type: 'success',
            duration: 5 * 1000
          })
          this.getList();
        }
      }).catch(err => {
        this.listLoading = false
        this.dialogVisible = false;
      })

    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .app-table {
    margin: 25px 100px 75px 100px
  }

  .addButton {
    margin: 0px 0px 20px 94%
  }

}

/* 只修改特定表格的表头颜色 */
.my-custom-table .el-table th {
  background-color: #F56C6C;
  /* 你想要的颜色 */
}
</style>
